import logging
import cgi
import os
import datetime

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from droidguide_classes import *

def saveUserProfile(self):
    self.response.out.write('here!')
    logging.debug('saving user profile')
    key_name = self.request.get('key')
    user_profile = UserProfile()
    if key_name:
      user_profile = db.get(db.Key(key_name))

    droidGuideUser_key = self.request.get(DROID_GUIDE_USER_KEY)
    logging.debug('droidGuideUser_key: %s' % droidGuideUser_key )

    if droidGuideUser_key:
        user_profile.name = self.request.get('name')
	user_profile.droidGuideUser_key = droidGuideUser_key
	user_profile.gender = self.request.get('gender')
      
        if self.request.get('birth_year'):
            user_profile.birth_year = int(self.request.get('birth_year'))
      
        user_profile.marital_status = self.request.get('marital_status')
      
        if self.request.get('food'):
            user_profile.food = bool(int(self.request.get('food')))
      
        if self.request.get('drink'):
            user_profile.drink = bool(int(self.request.get('drink')))
      
        if self.request.get('style'):
            user_profile.style = self.request.get('style')
      
        if self.request.get('consumer'):
            user_profile.consumer = int(self.request.get('consumer'))
      
        if self.request.get('historical'):
            user_profile.historical = int(self.request.get('historical'))
            
        if self.request.get('ecological'):
            user_profile.ecological = int(self.request.get('ecological'))
            
        if self.request.get('gastronomic'):
            user_profile.gastronomic = int(self.request.get('gastronomic'))
            
        if self.request.get('cultural'):
            user_profile.cultural = int(self.request.get('cultural'))
            
        if self.request.get('bohemian'):
            user_profile.bohemian = int(self.request.get('bohemian'))
      
        if self.request.get('weather'):
            user_profile.weather = bool(int(self.request.get('weather')))
      
        if self.request.get('traffic'):
            user_profile.traffic = bool(int(self.request.get('traffic')))

        signature = str(user_profile.consumer) + ',' + str(user_profile.historical) + ',' + str(user_profile.ecological) + ',' + str(user_profile.gastronomic) + ',' + str(user_profile.cultural) + ',' + str(user_profile.bohemian)

	user_profile.signature = signature
        db.put(user_profile)
        logging.debug('signature: %s' % signature )

        dgEvent = DroidGuideEvent()
        dgEvent.type = 'profile'
        dgEvent.droidGuideUser_key = droidGuideUser_key
        dgEvent.source = 'user'
        dgEvent.data = signature
        dgEvent.put()

        self.response.out.write(user_profile.to_xml())
    else:
        addMessage('DroidGuide Key is required', self)

def getUserProfile(self):
    key_name = self.request.get('key')
    if key_name:
        user_profile = db.get(db.Key(key_name))
    else:
        droidGuideUser_key = self.request.get(DROID_GUIDE_USER_KEY) 
        if droidGuideUser_key:
	    userProfile = getProfileForUser(self)
	    if userProfile:
	        self.response.out.write(userProfile.to_xml())    

def deleteProfile(self):
    userProfile = getUserProfile(self)
    if userProfile:
        userProfile.delete()

class Process(webapp.RequestHandler):
  def get(self):
      
      operation = self.request.get('operation')

      startDroidGuideMessage(self)

      if operation == 'save':
          saveUserProfile(self)

      elif operation == 'get':
          getUserProfile(self)

      elif operation == 'delete':
          deleteProfile(self)

      elif operation == 'list':
          listObjects(UserProfile, self)

      finishDroidGuideMessage(self)

application = webapp.WSGIApplication([('/user_profile/process', Process)], debug=True)

def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
